package inventory.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import inventory.entity.Repository;
import inventory.entity.User;
import inventory.entity.bo.QueryThingsInRepositoryCondition;
import inventory.entity.po.EquipmentTerminalRepositoryPo;
import inventory.entity.po.MaterialStuffRepositoryPo;
import inventory.mappers.*;
import inventory.service.RepositoryService;
import inventory.utils.UUIDUtils;
import inventory.utils.UnderlineWithHump;
import inventory.utils.UserRoleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sxt
 * @since 2019-11-24
 */
@Service
public class RepositoryServiceImpl extends ServiceImpl<RepositoryMapper, Repository> implements RepositoryService {
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    RepositoryMapper repositoryMapper;
    @Autowired
    EquipmentTerminalRepositoryPoMapper equipmentTerminalRepositoryPoMapper;
    @Autowired
    MaterialStuffRepositoryPoMapper materialStuffRepositoryPoMapper;
    @Autowired
    UserPoMapper userPoMapper;

    @Override
    public void distributionRepository(User user, String managerId) {

         String roleId=user.getRoleId();
        String status = roleMapper.queryStateByRoleId(roleId);
        if(status.equals("0")){
            Repository repository = new Repository();
            repository.setRepositoryId(UUIDUtils.getId());
            repository.setRepositoryName(user.getUsername()+"的仓库");
            repository.setBelongingStatus("0");
            repository.setUserId(user.getUserId());
            repositoryMapper.insert(repository);

        }
    }

    @Override
    public List<EquipmentTerminalRepositoryPo> selectEquipmentTerminalInRepository(QueryThingsInRepositoryCondition queryThingsInRepositoryCondition) {

        Map<String, Object> map = UnderlineWithHump.ObjectFieldNameToUnderline(queryThingsInRepositoryCondition);
        UserRoleUtil.judeUser(map);
        return equipmentTerminalRepositoryPoMapper.selectByMap(map);
    }

    @Override
    public List<MaterialStuffRepositoryPo> selectMaterialStuffRepository(QueryThingsInRepositoryCondition queryThingsInRepositoryCondition) {

        Map<String, Object> map = UnderlineWithHump.ObjectFieldNameToUnderline(queryThingsInRepositoryCondition);
        UserRoleUtil.judeUser(map);
        return materialStuffRepositoryPoMapper.selectByMap(map);
    }

    @Override
    public List<Repository> selectRepositoryByUserId(String userId) {
        Map<String,Object> map = new HashMap<>();
        map.put("user_id", userId);
        UserRoleUtil.judeUser(map);
        return repositoryMapper.selectByMap(map);
    }


}
